package cs275.ants.graph.antgraph;

import cs275.ants.graph.base.GraphEdge;

public class AntGraphEdge extends GraphEdge {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8402235905800277250L;
	double updateQuantity;
	static final double EVAPORATION_COEFFICIENT = .025; //0.01
	
	private static double maxSaturation = Long.MAX_VALUE;
	
	public AntGraphEdge(AntGraphVertex node1, AntGraphVertex node2) {
		this(node1, node2, 1); // Initialize cost at 1
	}
	
	public AntGraphEdge(AntGraphVertex node1, AntGraphVertex node2, double cost) {
		super(node1, node2, 1);
	}
	
	public void depositPheromone(double pheromone) {
		// Do something intelligent with the "cost" here.
		updateQuantity += pheromone;
	}
	
	public static void setMaxSaturation(double maxSaturation) {
		AntGraphEdge.maxSaturation = maxSaturation;
	}
	
	/**
	 * Update pheromone ONLY after all ants are done in timestep.
	 */
	public void updatePheromone() {
		if(getNode1() instanceof AntGraphVertex && getNode2() instanceof AntGraphVertex) {
			AntGraphVertex antVertex1 = (AntGraphVertex) getNode1();
			AntGraphVertex antVertex2 = (AntGraphVertex) getNode2();
			if(antVertex1.hasFood() || antVertex2.hasFood()) {
				updateQuantity = 0;
				setCost(Double.MAX_VALUE);
				return;
			}
		}
		
		double nextLevel = (1 - EVAPORATION_COEFFICIENT) * getCost();
		double cost = Math.max(1, nextLevel) + updateQuantity;
		cost = Math.min(maxSaturation, cost);
		setCost(cost);
		updateQuantity = 0;
	}
}
